home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / graphicgems4.lha / GemsIV / graph_layout / vector.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  2.2 KB  |  86 lines

  1. /*****************************************************************************
  2. **    TEST FILE FOR graph (Dynamic Layout Alg)
  3. **
  4. **    MODUL   - 2D VECTOR OPERATIONS
  5. **
  6. ** Author: dr. Szirmay-Kalos Laszlo (szirmay@fsz.bme.hu)
  7. **       Technical University of Budapest, Hungary
  8. *****************************************************************************/
  9. #include <math.h>
  10.  
  11. #ifdef MSWINDOWS
  12. #include "vector.hxx"
  13. #else
  14. #include "vector.hxx"
  15. #endif
  16.  
  17. /*------------------ Overloaded + operator  --------------------*/
  18. /* Adds two vectors                        */
  19. /* IN  : reference of the two operands                */
  20. /* OUT : result vector                        */
  21. /*--------------------------------------------------------------*/
  22. vector operator+(vector& a, vector& b)
  23. {
  24.     vector sum;
  25.     sum.x = a.x + b.x;
  26.     sum.y = a.y + b.y;
  27.     return sum;
  28. }
  29.  
  30. /*--------------- Overloaded binary - operator    ----------------*/
  31. /* Subtract two vectors                        */
  32. /* IN  : reference of the two operands                */
  33. /* OUT : result vector                        */
  34. /*--------------------------------------------------------------*/
  35. vector operator-(vector& a, vector& b)
  36. {
  37.     vector dif;
  38.     dif.x = a.x - b.x;
  39.     dif.y = a.y - b.y;
  40.     return dif;
  41. }
  42.  
  43. /*-------------- Overloaded unary - operator -------------------*/
  44. /* Negates a vector                        */
  45. /* IN  : operand                        */
  46. /* OUT : negated vector                        */
  47. /*--------------------------------------------------------------*/
  48. vector operator-(vector& a)
  49. {
  50.     vector neg;
  51.     neg.x = -a.x;
  52.     neg.y = -a.y;
  53.     return neg;
  54. }
  55.  
  56. /*------------------ Overloaded * operator  --------------------*/
  57. /* Multiplies a vector with a scalar                */
  58. /* IN  : vector and scalar operand                */
  59. /* OUT : result vector                        */
  60. /*--------------------------------------------------------------*/
  61. vector operator*(vector& a, double s)
  62. {
  63.     vector scaled;
  64.     scaled.x = s * a.x;
  65.     scaled.y = s * a.y;
  66.     return scaled;
  67. }
  68.  
  69. vector operator*(double s, vector& a)
  70. {
  71.     vector scaled;
  72.     scaled.x = s * a.x;
  73.     scaled.y = s * a.y;
  74.     return scaled;
  75. }
  76.  
  77. /*------------------------- Size -------------------------------*/
  78. /* Calculates the absolute value of the vector            */
  79. /* IN  : -                            */
  80. /* OUT : length                            */
  81. /*--------------------------------------------------------------*/
  82. double vector::Size()
  83. {
  84.     return sqrt( x * x + y * y );
  85. }
  86.